package com.ele.web;

import cn.hutool.core.util.StrUtil;
import com.ele.pojo.Business;
import com.ele.service.BusinessService;
import com.ele.service.impl.BusinessServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/businessLogin")
public class BusinessLoginServlet extends HttpServlet {
    private BusinessService businessService = new BusinessServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        if(StrUtil.isBlank(account) || StrUtil.isBlank(password)){
            // 这里可以返回不同的标记，前端根据标记给出对应的提示信息）
            // 0 ： 表示账号或密码为空
            // 1 ： 表示账号或密码错误
            // 2 ： 表示登录成功
            resp.getWriter().write(0 + "");
            return;
        }
        Business business = businessService.businessLogin(account, password);
        if(business == null){
            resp.getWriter().write(1 + "");
        }else{
            resp.setContentType("application/json;charset=utf-8");
            Gson gson = new Gson();
            String json = gson.toJson(business);
            resp.getWriter().write(json);
        }
    }
}
